{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import clear_output\n",
    "\n",
    "def display_board(board):\n",
    "    clear_output()\n",
    "    print(' '+board[7]+' | '+board[8]+' | '+board[9]+' ')\n",
    "    print('---------')\n",
    "    print(' '+board[4]+' | '+board[5]+' | '+board[6]+' ')\n",
    "    print('---------')\n",
    "    print(' '+board[1]+' | '+board[2]+' | '+board[3]+' ')\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "board = ['#','X','O','X','O','X','O','X','O','X','O']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " X | O | X \n",
      "---------\n",
      " O | X | O \n",
      "---------\n",
      " X | O | X \n"
     ]
    }
   ],
   "source": [
    "display_board(board)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def player_input():\n",
    "    marker = ''\n",
    "    \n",
    "    while not (marker == 'X' or marker == \"O\"):\n",
    "        marker = input(\"Enter a Marker X/O for player one : \").upper()\n",
    "    \n",
    "    if marker == 'X':\n",
    "        return ('X','O')\n",
    "    else:\n",
    "        return ('O','X')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a Marker X/O for player one : \n",
      "Enter a Marker X/O for player one : \n",
      "Enter a Marker X/O for player one : \n",
      "Enter a Marker X/O for player one : 1\n",
      "Enter a Marker X/O for player one : x\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "('X', 'O')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "player_input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def place_marker(board,marker,position):\n",
    "    board[position] = marker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "place_marker(board,\"X\",5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " X | O | X \n",
      "---------\n",
      " O | X | O \n",
      "---------\n",
      " X | O | X \n"
     ]
    }
   ],
   "source": [
    "display_board(board)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def win_check(board,marker):\n",
    "    \n",
    "    if( (board[7]==marker and board[8]==marker and board[9]==marker) or\n",
    "        (board[4]==marker and board[5]==marker and board[6]==marker) or\n",
    "        (board[1]==marker and board[2]==marker and board[3]==marker) or\n",
    "        (board[7]==marker and board[4]==marker and board[1]==marker) or\n",
    "        (board[8]==marker and board[5]==marker and board[2]==marker) or\n",
    "        (board[9]==marker and board[6]==marker and board[3]==marker) or\n",
    "        (board[7]==marker and board[5]==marker and board[3]==marker) or\n",
    "        (board[9]==marker and board[5]==marker and board[1]==marker)):\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "win_check(board,'X')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "win_check(board,'O')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random \n",
    "\n",
    "def choose_first():\n",
    "    num = random.randint(0,1)\n",
    "    \n",
    "    if num == 1:\n",
    "        return 'Player 1'\n",
    "    else:\n",
    "        return 'Player 2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Player 1'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "choose_first()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "board = ['#','X',' ',' ','O','X',' ','X',' ','X','O']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def space_check(board,position):\n",
    "    return board[position] == ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "space_check(board,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "def full_board_check(board):\n",
    "    isFull = True\n",
    "    for i in board:\n",
    "        if i == ' ':\n",
    "            isFull = False\n",
    "    return isFull\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "full_board_check(board)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "def full_board_check(board):\n",
    "    for i in range(1,10):\n",
    "        if space_check(board,i):\n",
    "            return False\n",
    "    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "full_board_check(board)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "def players_choice(board):\n",
    "    position = 0\n",
    "    \n",
    "    while not position in [1,2,3,4,5,6,7,8,9] or not space_check(board,position) : \n",
    "        position = int(input(\"Enter your next position : \"))\n",
    "        \n",
    "    return position"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter your next position : 4\n",
      "Enter your next position : 3\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "players_choice(board)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "def replay():\n",
    "    return input(\"Do you want to play again (Y/N) : \").lower().startswith('y')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Do you want to play again (Y/N) : y\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "replay()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " O |   |   \n",
      "---------\n",
      "   | O |   \n",
      "---------\n",
      " X | X | O \n",
      "Player 2 Won the game!! congratzz\n",
      "Do you want to play again (Y/N) : n\n"
     ]
    }
   ],
   "source": [
    "# Here Comes the final Assembly of the game \n",
    "\n",
    "\n",
    "while True:\n",
    "    \n",
    "    board = [' ']* 10\n",
    "    \n",
    "    \n",
    "    player1_marker, player2_marker = player_input()\n",
    "    turn = choose_first()\n",
    "    \n",
    "    print(turn + \" Will play First\")\n",
    "    \n",
    "    play_game = input(\"Are you ready to play the game Y/N\").lower().startswith('y')\n",
    "    \n",
    "    if play_game:\n",
    "        game_on = True\n",
    "    else:\n",
    "        game_on = False\n",
    "    \n",
    "    while game_on:\n",
    "        if turn == 'Player 1':\n",
    "            # Game logic starts here for player 1 \n",
    "            \n",
    "            display_board(board)\n",
    "            position = players_choice(board)\n",
    "            place_marker(board,player1_marker,position)\n",
    "            \n",
    "            if win_check(board,player1_marker):\n",
    "                display_board(board)\n",
    "                print(\"Player 1 Won the game!! congratzz\")\n",
    "                game_on = False\n",
    "            else:\n",
    "                if full_board_check(board):\n",
    "                    display_board(board)\n",
    "                    print('The Game is Draw, Better luck next time !!')\n",
    "                    break\n",
    "                else:\n",
    "                    turn = \"Player 2\"\n",
    "        else:\n",
    "            # Player 2 Logic \n",
    "            \n",
    "            display_board(board)\n",
    "            position = players_choice(board)\n",
    "            place_marker(board,player2_marker,position)\n",
    "            \n",
    "            if win_check(board,player2_marker):\n",
    "                display_board(board)\n",
    "                print(\"Player 2 Won the game!! congratzz\")\n",
    "                game_on = False\n",
    "            else:\n",
    "                if full_board_check(board):\n",
    "                    display_board(board)\n",
    "                    print('The Game is Draw, Better luck next time !!')\n",
    "                    break\n",
    "                else:\n",
    "                    turn = \"Player 1\"\n",
    "    \n",
    "    if not replay():\n",
    "        break "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
